package net.youpo.Market;

import net.youpo.Market.Download.DownloadService;
import net.youpo.Market.Service.MessageService;
import net.youpo.Market.Util.GlobalVariables;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

public class MarketApplication extends Application {
    private static Context sContext;

    public static Context getMarketAppContext() {
        return sContext;
    }

    public static void setMarketAppContext(Context context) {
        sContext = context;
    }

    public MarketApplication() {
        // setMarketAppContext(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setMarketAppContext(getApplicationContext());
        startDownloadService();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!GlobalVariables.IS_RUNNING) {
                    startMessageService();// 开启后台消息推送service
                }
            }
        }, 10000);// 延迟查看应用是否是否启动
    }

    public void startDownloadService() {
        Intent intent = new Intent();
        intent.setClass(this, DownloadService.class);
        startService(intent);
    }

    /**
     * 开启消息服务
     */
    private void startMessageService() {
        Intent intent = new Intent(MarketApplication.this, MessageService.class);
        startService(intent);
    }
}
